﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZDSoft.QLMS.Entity.CreateRule;
using ZDSoft.QLMS.Web.Helpers;
using ZDSoft.QLMS.Entity;
using ZDSoft.QLMS.Entity.Rules;
using NHibernate.Criterion;
namespace ZDSoft.QLMS.Web.Controllers
{
    public class StrategyController : ControllerBase
    {
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Create()
        {
            return View();
        }
        public ActionResult Update(int id)
        {
            IList<ICriterion> queryConditions = new List<ICriterion>();
            queryConditions.Add(Expression.Eq("PublishMan", AppHelper.LoginedUser));
            queryConditions.Add(Expression.Eq("SetQuestionTask", new SetQuestionTask().FindByID(id)));
            TestPaperRule tpr = new TestPaperRule().Find(queryConditions)[0];
            TempData["Score"] = tpr.Grade;
            TempData["Min"] = tpr.Min;
            return View();
        }
        [HttpPost]
        public void Update(int min, int taskID, float score, int courseID, string detail)
        {
            IList<ICriterion> queryConditions = new List<ICriterion>();
            queryConditions.Add(Expression.Eq("PublishMan", AppHelper.LoginedUser));
            queryConditions.Add(Expression.Eq("SetQuestionTask", new SetQuestionTask().FindByID(taskID)));
            TestPaperRule oldTpr = new TestPaperRule().Find(queryConditions)[0];
            oldTpr.Delete();

            IList<string> arr = detail.Split(',');
            IList<string> realArr = new List<string>();
            int tmp = 1;
            int flag = 1;
            foreach (string i in arr)
            {
                if (tmp <= 3)
                {
                    realArr.Add(i);
                }
                else
                {
                    string tmpStr = "";
                    IList<string> tmpList = realArr[flag * 3 - 1].Split('_');
                    for (int tmpi = 0; tmpi < tmpList.Count - 1; tmpi++)
                    {
                        tmpStr += tmpList[tmpi] + "_";
                    }
                    tmpStr = tmpStr.Substring(0, tmpStr.Length - 1);
                    tmpStr += ":" + realArr[flag * 3 - 1].Split(':')[1];
                    tmpStr += ":" + i.Split(':')[1];
                    realArr[flag * 3 - 1] = tmpStr;
                    tmp = 0;
                    flag++;
                }
                tmp++;
            }

            TestPaperRule tpr = new TestPaperRule();
            tpr.PublishMan = AppHelper.LoginedUser;
            tpr.SetQuestionTask = new SetQuestionTask().FindByID(taskID);
            tpr.Course = new Course().FindByID(courseID);
            tpr.Grade = score;
            tpr.isPass = false;
            tpr.Min = min;
            tpr.TypeRules = new List<TypeRule>();
            tpr.Create();
            foreach (string i in realArr)
            {
                double weight = double.Parse(i.Split(':')[1]);
                IList<string> subArr = i.Split(':')[0].Split('_');
                if (subArr.Count == 1)
                {
                    TypeRule typeRule = new TypeRule();
                    typeRule.Type = new ZDSoft.QLMS.Entity.Type().FindByID(int.Parse(subArr[0].Substring(4)));
                    typeRule.Weight = weight;
                    typeRule.TestPaperRule = tpr;
                    typeRule.KnowledgePointRules = new List<KnowledgePointRule>();
                    typeRule.Create();
                }
            }
            tpr = new TestPaperRule().FindByID(tpr.ID);
            foreach (string i in realArr)
            {
                IList<string> subArr = i.Split(':')[0].Split('_');
                if (subArr.Count == 2)
                {
                    double weight = double.Parse(i.Split(':')[1]);
                    foreach (TypeRule typeRule in tpr.TypeRules)
                    {
                        if (int.Parse(i.Split('_')[0].Substring(4)) == typeRule.Type.ID)
                        {
                            KnowledgePointRule klp = new KnowledgePointRule();
                            klp.Weight = weight;
                            klp.KnowledgePoint = new KnowledgePoint().FindByID(int.Parse(i.Split(':')[0].Split('_')[1].Substring(4)));
                            klp.TypeRule = typeRule;
                            klp.Details = new List<Detail>();
                            klp.Create();
                        }
                    }
                }
            }
            tpr = new TestPaperRule().FindByID(tpr.ID);
            Array diff = Enum.GetValues(typeof(DefficultDegree));
            foreach (string i in realArr)
            {
                IList<string> subArr = i.Split(':')[0].Split('_');
                if (subArr.Count == 3)
                {
                    double weight = double.Parse(i.Split(':')[1]);
                    foreach (TypeRule typeRule in tpr.TypeRules)
                    {
                        if (int.Parse(i.Split('_')[0].Substring(4)) == typeRule.Type.ID)
                        {
                            foreach (KnowledgePointRule klp in typeRule.KnowledgePointRules)
                            {
                                if (int.Parse(i.Split('_')[1].Substring(4)) == klp.KnowledgePoint.ID)
                                {
                                    Detail detailEntity = new Detail();
                                    detailEntity.Weight = weight;
                                    detailEntity.DefficultDegree = (DefficultDegree)diff.GetValue(int.Parse(i.Split('_')[2].Substring(4).Split(':')[0]) - 1);
                                    detailEntity.Num = int.Parse(i.Split('_')[2].Substring(4).Split(':')[1]);
                                    detailEntity.KnowledgePointRule = klp;
                                    detailEntity.Create();
                                }
                            }
                        }
                    }
                }
            }
            tpr = new TestPaperRule().FindByID(tpr.ID);
        }
        [HttpPost]
        public void Create(int taskID, float score, int courseID, string detail, int min)
        {
            IList<string> arr = detail.Split(',');
            IList<string> realArr = new List<string>();
            int tmp = 1;
            int flag = 1;
            foreach (string i in arr)
            {
                if (tmp <= 3)
                {
                    realArr.Add(i);
                }
                else
                {
                    string tmpStr = "";
                    IList<string> tmpList = realArr[flag * 3 - 1].Split('_');
                    for (int tmpi = 0; tmpi < tmpList.Count - 1; tmpi++)
                    {
                        tmpStr += tmpList[tmpi] + "_";
                    }
                    tmpStr = tmpStr.Substring(0, tmpStr.Length - 1);
                    tmpStr += ":" + realArr[flag * 3 - 1].Split(':')[1];
                    tmpStr += ":" + i.Split(':')[1];
                    realArr[flag * 3 - 1] = tmpStr;
                    tmp = 0;
                    flag++; 
                }
                tmp++;
            }

            TestPaperRule tpr = new TestPaperRule();
            tpr.PublishMan = AppHelper.LoginedUser;
            tpr.SetQuestionTask = new SetQuestionTask().FindByID(taskID);
            tpr.Course = new Course().FindByID(courseID);
            tpr.Grade = score;
            tpr.isPass = false;
            tpr.Min = min;
            tpr.TypeRules = new List<TypeRule>();
            tpr.Create();
            foreach (string i in realArr)
            {
                double weight = double.Parse(i.Split(':')[1]);
                IList<string> subArr = i.Split(':')[0].Split('_');
                if (subArr.Count == 1)
                {
                    TypeRule typeRule = new TypeRule();
                    typeRule.Type = new ZDSoft.QLMS.Entity.Type().FindByID(int.Parse(subArr[0].Substring(4)));
                    typeRule.Weight = weight;
                    typeRule.TestPaperRule = tpr;
                    typeRule.KnowledgePointRules = new List<KnowledgePointRule>();
                    typeRule.Create();
                }
            }
            tpr = new TestPaperRule().FindByID(tpr.ID);
            foreach (string i in realArr)
            {
                IList<string> subArr = i.Split(':')[0].Split('_');
                if (subArr.Count == 2)
                {
                    double weight = double.Parse(i.Split(':')[1]);
                    foreach (TypeRule typeRule in tpr.TypeRules)
                    {
                        if (int.Parse(i.Split('_')[0].Substring(4)) == typeRule.Type.ID)
                        {
                            KnowledgePointRule klp = new KnowledgePointRule();
                            klp.Weight = weight;
                            klp.KnowledgePoint = new KnowledgePoint().FindByID(int.Parse(i.Split(':')[0].Split('_')[1].Substring(4)));
                            klp.TypeRule = typeRule;
                            klp.Details = new List<Detail>();
                            klp.Create();
                        }
                    }
                }
            }
            tpr = new TestPaperRule().FindByID(tpr.ID);
            Array diff = Enum.GetValues(typeof(DefficultDegree));
            foreach (string i in realArr)
            {
                IList<string> subArr = i.Split(':')[0].Split('_');
                if (subArr.Count == 3)
                {
                    double weight = double.Parse(i.Split(':')[2]);
                    foreach (TypeRule typeRule in tpr.TypeRules)
                    {
                        if (int.Parse(i.Split('_')[0].Substring(4)) == typeRule.Type.ID)
                        {
                            foreach (KnowledgePointRule klp in typeRule.KnowledgePointRules)
                            {
                                if (int.Parse(i.Split('_')[1].Substring(4)) == klp.KnowledgePoint.ID)
                                {
                                    Detail detailEntity = new Detail();
                                    detailEntity.Weight = weight;
                                    detailEntity.DefficultDegree = (DefficultDegree)diff.GetValue(int.Parse(i.Split('_')[2].Substring(4).Split(':')[0]) - 1);
                                    detailEntity.Num = int.Parse(i.Split('_')[2].Substring(4).Split(':')[1]);
                                    detailEntity.KnowledgePointRule = klp;
                                    detailEntity.Create();
                                }
                            }
                        }
                    }
                }
            }
            //tpr = new TestPaperRule().FindByID(tpr.ID);
        }

        public string GetCourseNameByTask(int id)
        {
            SetQuestionTask entity = new SetQuestionTask().FindByID(id);
            return entity.Course.Name;
        }

        public JsonResult GetAllType()
        {
            IList<ZDSoft.QLMS.Entity.Type> types = new ZDSoft.QLMS.Entity.Type().FindAll();
            IList<ZTreeHelper<ZDSoft.QLMS.Entity.Type>.ZTreeNode> tree = new List<ZTreeHelper<ZDSoft.QLMS.Entity.Type>.ZTreeNode>();
            foreach (ZDSoft.QLMS.Entity.Type type in types)
            {
                ZTreeHelper<ZDSoft.QLMS.Entity.Type>.ZTreeNode node = new ZTreeHelper<ZDSoft.QLMS.Entity.Type>.ZTreeNode();
                node.id = type.ID;
                node.tld = "type" + type.ID;
                node.icon = "../../Content/img/diy/1_close.png";
                node.name = type.Name;
                tree.Add(node);
            }
            return this.Json(tree, JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetAllKnowledgePoint(int courseID)
        {
            Course course = new Course().FindByID(courseID);
            IList<KnowledgePoint> points = course.KnowledgePoints;
            IList<ZTreeHelper<KnowledgePoint>.ZTreeNode> tree = new List<ZTreeHelper<KnowledgePoint>.ZTreeNode>();
            foreach (KnowledgePoint point in points)
            {
                ZTreeHelper<KnowledgePoint>.ZTreeNode node = new ZTreeHelper<KnowledgePoint>.ZTreeNode();
                node.id = point.ID;
                node.tld = "know" + point.ID;
                node.icon = "../../Content/img/diy/1_close.png";
                node.name = point.Name;
                tree.Add(node);
            }
            return this.Json(tree, JsonRequestBehavior.AllowGet);
        }

        public string GetStrategyedByID(int taskId)
        {
            IList<ICriterion> queryConditions = new List<ICriterion>();
            queryConditions.Add(Expression.Eq("PublishMan", AppHelper.LoginedUser));
            queryConditions.Add(Expression.Eq("SetQuestionTask", new SetQuestionTask().FindByID(taskId)));
            TestPaperRule tpr = new TestPaperRule().Find(queryConditions)[0];
            string jsonStr = "[";
            foreach (TypeRule tr in tpr.TypeRules)
            {
                jsonStr += "{\"weight\":\""+tr.Weight+"\",\"id\":\"" + tr.ID + "\",\"tld\":\"tepy" + tr.Type.ID + "\",\"icon\":\"../../Content/img/diy/1_close.png\",\"name\":\"" + tr.Type.Name + "\"";
                if (tr.KnowledgePointRules != null && tr.KnowledgePointRules.Count != 0)
                {
                    jsonStr += ",\"children\":[";
                    foreach (KnowledgePointRule klp in tr.KnowledgePointRules)
                    {
                        jsonStr += "{\"weight\":\"" + klp.Weight + "\",\"id\":\"" + klp.ID + "\",\"tld\":\"know" + klp.KnowledgePoint.ID + "\",\"icon\":\"../../Content/img/diy/1_close.png\",\"name\":\"" + klp.KnowledgePoint.Name + "\"";
                        if (klp.Details != null && klp.Details.Count != 0)
                        {

                            jsonStr += ",\"children\":[";
                            foreach (Detail detail in klp.Details)
                            {
                                jsonStr += "{\"num\":\""+detail.Num+"\",\"weight\":\"" + detail.Weight + "\",\"id\":\"" + detail.ID + "\",\"tld\":\"diff" + (int)detail.DefficultDegree + "\",\"icon\":\"../../Content/img/diy/1_close.png\",\"name\":\"" + detail.DefficultDegree + "\"},";
                            }
                            jsonStr = jsonStr.Substring(0, jsonStr.Length - 1);
                            jsonStr += "]";
                        }
                        jsonStr += "},";
                    }
                    jsonStr = jsonStr.Substring(0, jsonStr.Length - 1);
                    jsonStr += "]";
                }
                jsonStr += "},";
            }
            jsonStr = jsonStr.Substring(0, jsonStr.Length - 1);
            jsonStr += "]";
            return jsonStr;
        }
    }
}
